#!/usr/bin/ruby

require 'test/unit'
require 'lib/rire'

class TestSort < Test::Unit::TestCase
  def setup
    @dsl = Rire::DSL.new($rire_home + '/plugin')
    @dsl.instance_eval <<-EOD
      def execute(src)
        instance_eval(src, 'unit.dsl', 1)
      end
    EOD
  end

  def test_no_key
    src = <<-SCENARIO
      define_task do
        subscribe.const :config => [
          { :key => 'aa', :val => 10 },
          { :key => 'dd', :val => 12 },
          { :key => 'bb', :val => 14 },
          { :key => 'aa', :val => 15 },
          { :key => 'cc', :val => 12 },
        ]

        filter.sort
      end
    SCENARIO

    ctx = @dsl.execute(src)

    assert_equal('aa', ctx.entries[0].key)
    assert_equal('dd', ctx.entries[1].key)
    assert_equal('bb', ctx.entries[2].key)
    assert_equal('aa', ctx.entries[3].key)
    assert_equal('cc', ctx.entries[4].key)
  end

  def test_no_order
    src = <<-SCENARIO
      define_task do
        subscribe.const :config => [
          { :key => 'aa', :val => 10 },
          { :key => 'dd', :val => 12 },
          { :key => 'bb', :val => 15 },
          { :key => 'ee', :val => 14 },
          { :key => 'cc', :val => 11 },
        ]

        filter.sort :config => { :key => 'val' }
      end
    SCENARIO

    ctx = @dsl.execute(src)

    assert_equal('bb', ctx.entries[0].key)
    assert_equal('ee', ctx.entries[1].key)
    assert_equal('dd', ctx.entries[2].key)
    assert_equal('cc', ctx.entries[3].key)
    assert_equal('aa', ctx.entries[4].key)

    assert_equal(15, ctx.entries[0].val)
    assert_equal(14, ctx.entries[1].val)
    assert_equal(12, ctx.entries[2].val)
    assert_equal(11, ctx.entries[3].val)
    assert_equal(10, ctx.entries[4].val)
  end

  def test_order_asc
    src = <<-SCENARIO
      define_task do
        subscribe.const :config => [
          { :key => 'aa', :val => 10 },
          { :key => 'dd', :val => 12 },
          { :key => 'bb', :val => 15 },
          { :key => 'ee', :val => 14 },
          { :key => 'cc', :val => 11 },
        ]

        filter.sort :config => { :key => 'val', :order => 'asc' }
      end
    SCENARIO

    ctx = @dsl.execute(src)

    assert_equal('aa', ctx.entries[0].key)
    assert_equal('cc', ctx.entries[1].key)
    assert_equal('dd', ctx.entries[2].key)
    assert_equal('ee', ctx.entries[3].key)
    assert_equal('bb', ctx.entries[4].key)

    assert_equal(10, ctx.entries[0].val)
    assert_equal(11, ctx.entries[1].val)
    assert_equal(12, ctx.entries[2].val)
    assert_equal(14, ctx.entries[3].val)
    assert_equal(15, ctx.entries[4].val)
  end

  def test_order_desc
    src = <<-SCENARIO
      define_task do
        subscribe.const :config => [
          { :key => 'aa', :val => 10 },
          { :key => 'dd', :val => 12 },
          { :key => 'bb', :val => 15 },
          { :key => 'ee', :val => 14 },
          { :key => 'cc', :val => 11 },
        ]

        filter.sort :config => {
          :key => 'val', :order => 'desc'
        }
      end
    SCENARIO

    ctx = @dsl.execute(src)

    assert_equal('bb', ctx.entries[0].key)
    assert_equal('ee', ctx.entries[1].key)
    assert_equal('dd', ctx.entries[2].key)
    assert_equal('cc', ctx.entries[3].key)
    assert_equal('aa', ctx.entries[4].key)

    assert_equal(15, ctx.entries[0].val)
    assert_equal(14, ctx.entries[1].val)
    assert_equal(12, ctx.entries[2].val)
    assert_equal(11, ctx.entries[3].val)
    assert_equal(10, ctx.entries[4].val)
  end

  def test_order_block
    src = <<-SCENARIO
      define_task do
        subscribe.const :config => [
          { :key => 'aa', :val => 10 },
          { :key => 'dd', :val => 12 },
          { :key => 'bb', :val => 15 },
          { :key => 'ee', :val => 14 },
          { :key => 'cc', :val => 11 },
        ]

        filter.sort do |a, b|
          a.val <=> b.val
        end
      end
    SCENARIO

    ctx = @dsl.execute(src)

    assert_equal('aa', ctx.entries[0].key)
    assert_equal('cc', ctx.entries[1].key)
    assert_equal('dd', ctx.entries[2].key)
    assert_equal('ee', ctx.entries[3].key)
    assert_equal('bb', ctx.entries[4].key)

    assert_equal(10, ctx.entries[0].val)
    assert_equal(11, ctx.entries[1].val)
    assert_equal(12, ctx.entries[2].val)
    assert_equal(14, ctx.entries[3].val)
  end

  def teardown
  end
end

